//
//  AccountInterface.m
//  DaJiang
//
//  Created by sally on 16/10/12.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "AccountInterface.h"

@implementation AccountInterface

- (id)init {
  self = [super initWithPath:@"/user"];
  if (self) {
  }
  return self;
}

#pragma mark - 账户相关
//发送验证码
- (void)sendCaptchaWithPhone:(NSString *)phone
                  completion:(completion_t_code)completion
                       error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"get_captcha.json");

  NSDictionary *parameters = @{ @"phone" : phone,
  };

  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        BOOL isSuccess = [[resultDict objectForKey:DicKeySmsSent] boolValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description, isSuccess);
      }
    }
  };

  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };

  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

//验证验证码并注册
- (void)registerWithPhone:(NSString *)phone
                  captcha:(NSString *)captcha
                 password:(NSString *)password
               completion:(completion_t_user)completion
                    error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"create_by_phone.json");

  NSDictionary *parameters = @{ @"phone" : phone,
                                @"captcha" : captcha,
                                @"password" : password,
  };

  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyUser];
        BOOL isNewUser = [[resultDict objectForKey:DicKeyNewUser]boolValue];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description,isNewUser);
      }
    }
  };

  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };

  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

//登录
- (void)loginWithPhone:(NSString *)phone
              password:(NSString *)password
            completion:(completion_t)completion
                 error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"login_by_phone.json");

  NSDictionary *parameters = @{ @"phone" : phone,
                                @"password" : password,
  };

  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };

  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };

  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

//完善用户资料
- (void)updateUserInfoWithUserName:(NSString *)userName
                    userProfilePic:(NSString *)userProfilePic
                      locationName:(NSString *)locationName
                            bigPic:(NSString *)bigPic
                           proDesc:(NSString *)proDesc
                          proTitle:(NSString *)proTitle
                        topicIndex:(NSString *)topicIndex
                       topicIndex2:(NSString *)topicIndex2
                        completion:(completion_t)completion
                             error:(error_t)error {
  self.interfaceURL = [[NSURL alloc] initWithScheme:Scheme host:[ApiBase shareInstance].host path:@"/user_info"];
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"update.json");
  UserModel *userModel = [[CacheManager shareInstance]getLastLoginAccount];
  
  NSDictionary *parameters = @{ @"uid" : @(userModel.uid),
                                @"user_name" : userName,
                                @"user_profile_pic" : userProfilePic,
                                @"user_location_name" : locationName,
                                @"user_profile_big_pic" : bigPic,
                                @"user_pro_desc" : proDesc,
                                @"user_pro_title" : proTitle,
                                @"user_pro_topic_index" : topicIndex,
                                @"user_pro_topic_index2" : topicIndex2,
                                };
  
  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };
  
  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };
  
  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

//设置用户密码
- (void)modifyPswWithPhone:(NSString *)phone
               oldPassword:(NSString *)oldPassword
                  password:(NSString *)password
                confirmPwd:(NSString *)confirmPwd
                completion:(completion_t)completion
                     error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"set_password.json");

  NSDictionary *parameters = @{ @"phone" : phone,
                                @"old_password" : oldPassword,
                                @"password" : password,
                                @"confirm_pwd" : confirmPwd,
  };

  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };

  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };

  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

//重置用户密码
- (void)resetPswWithPhone:(NSString *)phone
                 password:(NSString *)password
                  captcha:(NSString *)captcha
               completion:(completion_t)completion
                    error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"reset_password.json");
  
  NSDictionary *parameters = @{ @"phone" : phone,
                                @"password" : password,
                                @"captcha" : captcha,
                                };
  
  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };
  
  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };
  
  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

//信鸽注册设备
- (void)registerDeviceWithDeviceId:(NSString *)deviceId
                        completion:(completion_t)completion
                             error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"device/register.json");
  UserModel *userModel = [[CacheManager shareInstance]getLastLoginAccount];
  
  NSDictionary *parameters = @{ @"device_id" : deviceId,
                                @"device_platform" : @"ios",
                                @"uid" : @(userModel.uid),
                                };
  
  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };
  
  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };
  
  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

//信鸽注销设备
- (void)deleteDeviceWithDeviceId:(NSString *)deviceId
                      completion:(completion_t)completion
                           error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"device/delete.json");
  UserModel *userModel = [[CacheManager shareInstance]getLastLoginAccount];
  
  NSDictionary *parameters = @{ @"device_id" : deviceId,
                                @"uid" : @(userModel.uid),
                                };
  
  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };
  
  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };
  
  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

//上传图片
- (void)uploadPicWithImage:(UIImage *)image
                completion:(completion_t)completion
                     error:(error_t)error {
  self.interfaceURL = [[NSURL alloc] initWithScheme:Scheme host:[ApiBase shareInstance].host path:@"/util"];
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"file/upload.json");
  
  
  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };
  
  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };
  
  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    
    /**
     *  FileData:要上传的文件的二进制数据
     *  name:上传参数名称
     *  fileName：上传到服务器的文件名称
     *  mimeType：文件类型
     */
    if (image) {
      // FIX ME: 上传图片未压缩
      NSData *data = UIImagePNGRepresentation(image);
      //NSString *name = [NSString stringWithFormat:@"img_upload_%d.png", i];
      [formData appendPartWithFileData:data name:@"pic" fileName:@"img_upload.png" mimeType:@"application/octet-stream"];
    }
    
  } success:netCompletion failure:netFailure];
  
}

#pragma mark - 专家详情页相关
//收藏专家到心愿单
- (void)collectExpertWithProUid:(NSInteger)proUid
                      completion:(completion_t)completion
                           error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"like_pro_user.json");
  UserModel *userModel = [[CacheManager shareInstance]getLastLoginAccount];
  
  NSDictionary *parameters = @{@"pro_uid" : @(proUid),
                               @"uid" : @(userModel.uid),
                               };
  
  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };
  
  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };
  
  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

//取消收藏专家
- (void)cancelCollectExpertWithProUid:(NSInteger)proUid
                           completion:(completion_t)completion
                                error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"unlike_pro_user.json");
  UserModel *userModel = [[CacheManager shareInstance]getLastLoginAccount];
  
  NSDictionary *parameters = @{@"pro_uid" : @(proUid),
                               @"uid" : @(userModel.uid),
                               };
  
  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };
  
  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };
  
  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

#pragma mark - 约见接口相关
//用户选择专家有空时间
- (void)chooseFreeWithFreeId:(NSInteger)freeId
                   consultId:(NSInteger)consultId
                    meetTime:(NSString *)meetTime
                     meetLoc:(NSString *)meetLoc
                  completion:(completion_t)completion
                       error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"pro_user_free_time/choose.json");
  
  NSDictionary *parameters = @{@"free_time_id" : @(freeId),
                               @"consult_id" : @(consultId),
                               @"meet_time" : meetTime,
                               @"meet_location" : meetLoc,
                               };
  
  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };
  
  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };
  
  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

#pragma mark - 我的（用户）相关
//我约的行家
- (void)queryMyOrderListWithCursor:(NSInteger)cursor
                              type:(OrderStatusType)type
                        completion:(completion_t)completion
                             error:(error_t)error {
  NSURL *url;
  if (type == OrderStatusType_Ing) {
    url = CREATE_REAL_URL(self.interfaceURL, @"my_consulting_list/in_process.json");
  } else if (type == OrderStatusType_Comment) {
    url = CREATE_REAL_URL(self.interfaceURL, @"my_consulting_list/to_be_commented.json");
  } else if (type == OrderStatusType_End) {
    url = CREATE_REAL_URL(self.interfaceURL, @"my_consulting_list/closed.json");
  }
  UserModel *userModel = [[CacheManager shareInstance] getLastLoginAccount];
  
  NSDictionary *parameters;
  if (cursor > GCursorInit) {
    parameters = @{ @"uid" : @(userModel.uid),
                    @"cursor" : @(cursor),
                    };
  } else {
    parameters = @{ @"uid" : @(userModel.uid),
                    };
  }
  
  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };
  
  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };
  
  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

//我的心愿单
- (void)queryCollectListWithCursor:(NSInteger)cursor
                        completion:(completion_t)completion
                             error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"my_like_pro_user.json");
  UserModel *userModel = [[CacheManager shareInstance]getLastLoginAccount];
  
  NSDictionary *parameters;
  if (cursor > GCursorInit) {
    parameters = @{ @"uid" : @(userModel.uid),
                    @"cursor" : @(cursor),
                    };
  } else {
    parameters = @{@"uid" : @(userModel.uid),
                   };
  }
  
  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };
  
  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };
  
  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

//用户反馈
- (void)submitFeedbackWithPhone:(NSString *)phone
                        content:(NSString *)content
                     completion:(completion_t)completion
                          error:(error_t)error {
  self.interfaceURL = [[NSURL alloc] initWithScheme:Scheme host:[ApiBase shareInstance].host path:@"/user_feedback"];
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"new.json");
  UserModel *userModel = [[CacheManager shareInstance]getLastLoginAccount];
  
  NSDictionary *parameters = @{ @"uid" : @(userModel.uid),
                                @"content" : content,
                                @"user_name" : userModel.user_data.user_name,
                                @"phone_type" : @"ios",
                                @"version" : [[CommonMethod sharedCommonMethod]getLocalVersion],
                                @"contact_phone" : phone,
                                };
  
  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };
  
  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };
  
  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}

//当前用户信息接口
- (void)quaryUserInfoWithCompletion:(completion_t)completion
                              error:(error_t)error {
  NSURL *url = CREATE_REAL_URL(self.interfaceURL, @"show.json");
  UserModel *userModel = [[CacheManager shareInstance]getLastLoginAccount];
  
  NSDictionary *parameters = @{ @"uid" : @(userModel.uid),
                                };
  
  netCompletion_t netCompletion = ^(AFHTTPRequestOperation *operation, id responseObject) {
    NSDictionary *resultDict = responseObject;
    if (resultDict) {
      if (completion) {
        NSDictionary *resultObject = [resultDict objectForKey:DicKeyResult];
        NSInteger code = [[resultDict objectForKey:DicKeyErrorCode] integerValue];
        NSString *description = [resultDict objectForKey:DicKeyErrorMsg];
        description = description ? description : @"";
        completion(resultObject, code, description);
      }
    }
  };
  
  netFailure_t netFailure = ^(AFHTTPRequestOperation *option, NSError *e) {
    if (error) {
      error(e);
    }
  };
  
  AFHTTPRequestOperationManager *manager = [ApiBase shareInstance].shareManager;
  [manager POST:[url absoluteString] parameters:parameters success:netCompletion failure:netFailure];
}


@end


